package com.dmxh.service.impl;

import com.dmxh.mapper.TbSeckillGoodsMapper;
import com.dmxh.mapper.TbSeckillOrderMapper;
import com.dmxh.pojo.TbSeckillGoods;
import com.dmxh.pojo.TbSeckillOrder;
import com.dmxh.result.Result;
import com.dmxh.result.ResultConstant;
import com.dmxh.service.SeckillOrderService;
import com.dmxh.util.IdWorker;
import com.dmxh.vo.SeckillGoodsVo;
import com.dmxh.vo.SeckillOrderVo;
import com.dmxh.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

@Service
public class SeckillOrderServiceImpl implements SeckillOrderService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private TbSeckillOrderMapper seckillOrderMapper;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private TbSeckillGoodsMapper seckillGoodsMapper;


    @Override
    public Result rush(UserVO userVO, Long goodsId) {
        SeckillGoodsVo seckillGoodsVo = (SeckillGoodsVo) redisTemplate.boundHashOps("secKillGoodsList").get(goodsId);
        if (Objects.isNull(seckillGoodsVo)) {
            throw new RuntimeException("缓存中秒杀数据为空");
        }
        long endTime = seckillGoodsVo.getEndTime().getTime();
        long nowTime = new Date().getTime();
        if (endTime - nowTime <= 0) {
            throw new RuntimeException("秒杀时间结束了");
        }
        if (seckillGoodsVo.getStockCount() == 0) {
            //更新mysql库存  将数据库库存改成0
            TbSeckillGoods tbSeckillGoods = new TbSeckillGoods();
            tbSeckillGoods.setStockCount(0);
            seckillGoodsMapper.updateByPrimaryKeySelective(tbSeckillGoods);
            throw new RuntimeException("库存不足");
        } else {
            seckillGoodsVo.setStockCount(seckillGoodsVo.getStockCount() - 1);
            redisTemplate.boundHashOps("secKillGoodsList").put(seckillGoodsVo.getGoodsId(), seckillGoodsVo);
        }
        SeckillOrderVo seckillOrderVo = new SeckillOrderVo();
        seckillOrderVo.setId(idWorker.nextId());
        seckillOrderVo.setSeckillId(goodsId);
        seckillOrderVo.setCreateTime(new Date());
        seckillOrderVo.setMoney(seckillGoodsVo.getCostPrice());
        seckillOrderVo.setSellerId(seckillGoodsVo.getSellerId());
        seckillOrderVo.setStatus("0");
        seckillOrderVo.setUserId(userVO.getUsername());

        TbSeckillOrder tbSeckillOrder = new TbSeckillOrder();
        BeanUtils.copyProperties(seckillOrderVo, tbSeckillOrder);
        //生成订单最好是异步操作   mq
        //如果减完库存  但是没有生成订单怎么办  使用事务 or 库存锁定  or 检查订单生成情况如果订单生成失败 那就 将库存还原
        
        if (seckillOrderMapper.insertSelective(tbSeckillOrder) > 0) {//ok
            return Result.ok();
        } else {
            //no ok
            seckillGoodsVo.setStockCount(seckillGoodsVo.getStockCount() + 1);
            redisTemplate.boundHashOps("secKillGoodsList").put(seckillGoodsVo.getGoodsId(), seckillGoodsVo);
            return Result.response(ResultConstant.ORDER_FAIL_STOKCOUNT_BRFORE);
        }
    }
}
